package com.ruoyi.web.controller.inventory;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.TWarehouseTransaction;
import com.ruoyi.inventory.domain.dto.TWarehouseTransactionDTO;
import com.ruoyi.inventory.service.ITWarehouseTransactionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出入库单(头)Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/system/transaction")
public class TWarehouseTransactionController extends BaseController
{
    @Autowired
    private ITWarehouseTransactionService tWarehouseTransactionService;

    /**
     * 查询出入库单(头)列表
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseTransaction tWarehouseTransaction)
    {
        startPage();
        List<TWarehouseTransactionDTO> list = tWarehouseTransactionService.selectTWarehouseTransactionDTOList(tWarehouseTransaction);
        return getDataTable(list);
    }

    /**
     * 导出出入库单(头)列表
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:export')")
    @Log(title = "出入库单(头)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TWarehouseTransaction tWarehouseTransaction)
    {
        List<TWarehouseTransaction> list = tWarehouseTransactionService.selectTWarehouseTransactionList(tWarehouseTransaction);
        ExcelUtil<TWarehouseTransaction> util = new ExcelUtil<TWarehouseTransaction>(TWarehouseTransaction.class);
        util.exportExcel(response, list, "出入库单(头)数据");
    }

    /**
     * 获取出入库单(头)详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:query')")
    @GetMapping(value = "/{txNo}")
    public AjaxResult getInfo(@PathVariable("txNo") String txNo)
    {
        return success(tWarehouseTransactionService.selectTWarehouseTransactionByTxNo(txNo));
    }

    /**
     * 新增出入库单(头)
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:add')")
    @Log(title = "出入库单(头)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWarehouseTransaction tWarehouseTransaction)
    {
        // 提取备件列表
        List<Object> partList = tWarehouseTransaction.getPartList();
        // 清空主对象中的partList，避免数据库插入问题
        tWarehouseTransaction.setPartList(null);
        
        // 调用完整交易处理方法
        return toAjax(tWarehouseTransactionService.insertCompleteTransaction(tWarehouseTransaction, partList));
    }

    /**
     * 修改出入库单(头)
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:edit')")
    @Log(title = "出入库单(头)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWarehouseTransaction tWarehouseTransaction)
    {
        return toAjax(tWarehouseTransactionService.updateTWarehouseTransaction(tWarehouseTransaction));
    }

    /**
     * 删除出入库单(头)
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:remove')")
    @Log(title = "出入库单(头)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{txNos}")
    public AjaxResult remove(@PathVariable String[] txNos)
    {
        return toAjax(tWarehouseTransactionService.deleteTWarehouseTransactionByTxNos(txNos));
    }
}
